package com.wbdgj.ui.branch;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wbdgj.R;
import com.wbdgj.adapter.CompaignAdapter;
import com.wbdgj.adapter.MessageAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.NetFreeBranchModel;
import com.wbdgj.model.ResultMessageObjectModel;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.booking.BookingActivity;
import com.wbdgj.ui.home.CampaignDetailActivity;
import com.wbdgj.ui.home.MapDialogAtivity;
import com.wbdgj.ui.home.MessageActivity;
import com.wbdgj.ui.home.MessageListActivity;
import com.wbdgj.ui.home.WbDetailActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.near.MessageBoardDetailActivity;
import com.wbdgj.ui.word_compaign.WordCompaignActivity;
import com.wbdgj.utils.HorizontalListView;
import com.wbdgj.utils.ListViewForScrollView;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;
import com.wbdgj.views.RichTextView;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit2.Call;

public class BranchDetailActivity extends BaseActivity implements PtrHandler {

    private Context context = this;
    @BindView(R.id.pfl_root)
    PtrClassicFrameLayout pfl_root;

    @BindView(R.id.scrollView)
    ScrollView scrollView;

    @BindView(R.id.collection)
    ImageView collection;

    @BindView(R.id.ls)
    TextView ls;
    @BindView(R.id.ls2)
    TextView ls2;
    @BindView(R.id.ls3)
    TextView ls3;
    @BindView(R.id.name)
    TextView name;

    @BindView(R.id.BRANCH_AVG)
    TextView BRANCH_AVG;
    @BindView(R.id.BRANCH_AVG_TXT)
    TextView BRANCH_AVG_TXT;
    @BindView(R.id.numLy)
    TextView numLy;
    @BindView(R.id.yhly)
    TextView yhly;
    @BindView(R.id.mdss)
    RichTextView mdss;

    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.phone)
    TextView phoneTxt;

    @BindView(R.id.nameId)
    TextView nameId;
    @BindView(R.id.statusId)
    TextView statusId;
    @BindView(R.id.yeId)
    TextView yeId;
    @BindView(R.id.jqhTxt)
    TextView jqhTxt;

    @BindView(R.id.zdList)
    HorizontalListView zdList;
    private CompaignAdapter compaignAdapter;
    private MessageAdapter messageAdapter;

    private HashMap<String, NetFreeBranchModel> netFreeBranchModelHashMap;
    private NetFreeBranchModel netFreeBranchModel;
    private boolean pTrue  = true;

    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    private ArrayList<String> localImages = new ArrayList<String>();
    @BindView(R.id.mdImg)
    ImageView mdImg;

    @BindView(R.id.demo_listview)
    ListViewForScrollView demo_listview;


    private String BRANCH_ID, DISTANCE, REMARK;

    private String phone;
    private String TYPE, zoneName;
    private String COORDSX, COORDSY;
    private boolean isSj = false;

    private String nowTime;

    private String appId = "wxf1069c4f961f956e"; // 填应用AppId
    private IWXAPI api;
    private  WXLaunchMiniProgram.Req req;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_branch_detail;
    }

    @Override
    protected void initView() {

        api = WXAPIFactory.createWXAPI(context, appId);
        req = new WXLaunchMiniProgram.Req();

        //传值
        BRANCH_ID = getIntent().getStringExtra("id");
        DISTANCE = getIntent().getStringExtra("DISTANCE");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date dt = new Date();
        nowTime = sdf.format(dt);
        netFreeBranchModel = new NetFreeBranchModel();
        netFreeBranchModelHashMap = new HashMap<String, NetFreeBranchModel>();
        //时间判断是否需要请求接口
        if (BaseApplication.getInstance().getNetFreeBranchModelHashMap() == null){
            Log.e("zp", "11");
            rocordgroupa();
        }else{
            for(Iterator it = BaseApplication.getInstance().getNetFreeBranchModelHashMap().keySet().iterator(); it.hasNext();){
                String key = it.next().toString();
                netFreeBranchModelHashMap.put(key, BaseApplication.getInstance().getNetFreeBranchModelHashMap().get(key));
            }
            if (netFreeBranchModelHashMap.containsKey(BRANCH_ID)){
                netFreeBranchModel = netFreeBranchModelHashMap.get(BRANCH_ID);
                if (netFreeBranchModel.getToken().equals(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN))){
                    findViewById(R.id.mdhyLay).setVisibility(View.VISIBLE);
                    pTrue = false;
                    nameId.setText(netFreeBranchModel.getName()+"");
                    yeId.setText(netFreeBranchModel.getLevel()+"，余额：￥"+netFreeBranchModel.getRemain());
                    statusId.setText(netFreeBranchModel.getStatus());
                    jqhTxt.setText(netFreeBranchModel.getComputer());

                    Log.e("zp", "时间="+netFreeBranchModel.getTime());
                }else{
                    rocordgroupa();
                }
            }else{
                rocordgroupa();
            }
        }
        queryBranch();
        appbranchactivity();
        msgList();
        pfl_root.setEnabledNextPtrAtOnce(true);
        pfl_root.setLastUpdateTimeRelateObject(this);
        pfl_root.setPtrHandler(this);
        pfl_root.setKeepHeaderWhenRefresh(true);
        pfl_root.disableWhenHorizontalMove(true);

        zdList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                if (!compaignAdapter.hasNoData){
                    startActivity(new Intent(context, WordCompaignActivity.class).putExtra("id", compaignAdapter.getItem(i).get("cloud_id")+""));
                }
            }
        });

        demo_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!messageAdapter.hasNoData){
                    BaseApplication.getInstance().setLinkedTreeMap(messageAdapter.getItem(i));
                    startActivity(new Intent(context, MessageBoardDetailActivity.class).putExtra("branch_id", BRANCH_ID));
                }
            }
        });
    }

    @OnClick({R.id.mdjs, R.id.wfmy, R.id.ycxj, R.id.moreLy, R.id.moreHD, R.id.lyMore, R.id.dh, R.id.wylyLay, R.id.call, R.id.collection, R.id.mdIcon , R.id.login_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mdIcon:
//                req.userName = "gh_e174a0f94112"; // 填小程序原始id
//                req.path = "";                  //拉起小程序页面的可带参路径，不填默认拉起小程序首页
//                req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;//正式版
////                req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;//体验版
//                api.sendReq(req);
                BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.HOTEL_ID, BRANCH_ID);
                BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.HOTEL_NAME, name.getText().toString());
                startActivity(new Intent(context, BookingActivity.class));
                break;
            case R.id.login_btn:
//                req.userName = "gh_e174a0f94112"; // 填小程序原始id
//                req.path = "";                  //拉起小程序页面的可带参路径，不填默认拉起小程序首页
//                req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 正式版
////                req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;// 体验版
//                api.sendReq(req);
                BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.HOTEL_ID, BRANCH_ID);
                BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.HOTEL_NAME, name.getText().toString());
                startActivity(new Intent(context, BookingActivity.class));
                break;
            case R.id.mdjs:
                startActivity(new Intent(context, MdjrActivity.class).putExtra("REMARK", REMARK));
                break;
            case R.id.wfmy:
                startActivityForResult(new Intent(context, NetfeeRoamingActivity.class).putExtra("id", BRANCH_ID)
                        .putExtra("name", zoneName),1);
                break;
            case R.id.ycxj:
                if (!isSj){
                    ToastUtils.toastShort("您不在上机状态，无法进行下机操作！");
                    return;
                }
                final ConfirmDialog confirmDialogbalance = new ConfirmDialog(context, "确定下机吗？", 1);
                confirmDialogbalance.show();
                confirmDialogbalance.setCanceledOnTouchOutside(false);
                confirmDialogbalance.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm() {
                        confirmDialogbalance.dismiss();
                        balance();
                    }

                    @Override
                    public void doCancel() {
                        confirmDialogbalance.dismiss();
                    }
                });
                break;
            case R.id.moreHD:
                startActivity(new Intent(context, MoreCampaignActivity.class).putExtra("id", BRANCH_ID));
                break;
            case R.id.moreLy:
                startActivity(new Intent(context, MessageListActivity.class).putExtra("id", BRANCH_ID));
                break;
            case R.id.lyMore:
                startActivity(new Intent(context, MessageListActivity.class).putExtra("id", BRANCH_ID));
                break;
            case R.id.dh:
                if (TextUtils.isEmpty(COORDSX)){
                    Uri uri = Uri.parse("amapuri://route/plan/?dlat="+""+"&dlon="+""+"&dname="+name.getText().toString()+"&dev=0&t=0");
                    startActivity(new Intent(Intent.ACTION_VIEW, uri));
                }else{
                    startActivity(new Intent(context, MapDialogAtivity.class).putExtra("COORDSX", COORDSX).putExtra("COORDSY", COORDSY));
                }
                break;
            case R.id.wylyLay:
                startActivityForResult(new Intent(context, MessageActivity.class).putExtra("id", BRANCH_ID), 5);
                break;
            case R.id.collection:
                if (TYPE.equals("")){
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定关注吗？", 1);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                            addAttention();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }else{
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定取消关注吗？", 1);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                            delAttention();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }
                break;
            case R.id.call:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (ContextCompat.checkSelfPermission(context,
                            Manifest.permission.CALL_PHONE)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 10);
                    }else{
                        Intent intent =new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));
                        startActivity(intent);
                    }
                }else{
                    Intent intent =new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));
                    startActivity(intent);
                }
                break;
        }
    }

    private void queryBranch() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().queryBranch(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();

                    if (linkedTreeMap.containsKey("REMARK")){
                        REMARK = linkedTreeMap.get("REMARK")+"";
                    }else{
                        REMARK = "暂无介绍";
                    }

                    if (linkedTreeMap.containsKey("SPECIALITY")){
                       mdss.setRichText(linkedTreeMap.get("SPECIALITY")+"");
                    }else{
                        mdss.setRichText("暂无信息");
                    }

                    if (linkedTreeMap.containsKey("COORDSY")){
                        COORDSY =  linkedTreeMap.get("COORDSY")+"";
                    }
                    if (linkedTreeMap.containsKey("COORDSX")){
                        COORDSX =  linkedTreeMap.get("COORDSX")+"";
                    }
                    zoneName = linkedTreeMap.get("NAME") +"";
                    if (linkedTreeMap.containsKey("BRANCH_PHONE")){
                        phone = linkedTreeMap.get("BRANCH_PHONE") +"";
                        phoneTxt.setText("电话："+ linkedTreeMap.get("BRANCH_PHONE"));
                    }else{
                        phoneTxt.setText("电话：");
                    }

                    if (linkedTreeMap.containsKey("ADDRESS")){
                        address.setText("地址："+linkedTreeMap.get("ADDRESS")+"");
                    }else{
                        address.setText("地址：");
                    }


                    if ((linkedTreeMap.get("TYPE")+"").equals("null")){
                        TYPE = "";
                        collection.setImageResource(R.mipmap.branch_detail_collection);
                    }else{
                        TYPE = "1";
                        collection.setImageResource(R.mipmap.branch_detail_collection_f);
                    }

                    if (!(linkedTreeMap.get("BRANCH_AVG")+"").equals("null")){
                        BRANCH_AVG.setText(decimalFormat.format(linkedTreeMap.get("BRANCH_AVG"))+"元/小时");
                    }else{
                        BRANCH_AVG.setVisibility(View.GONE);
                        BRANCH_AVG_TXT.setVisibility(View.GONE);
                    }

                    if(linkedTreeMap.containsKey("BRANCH_ATTRIBUTE")){
                        if(decimalFormat.format(linkedTreeMap.get("BRANCH_ATTRIBUTE")).equals("0")){
                            findViewById(R.id.djjdLay).setVisibility(View.GONE);
                        }else{
                            findViewById(R.id.djjdLay).setVisibility(View.VISIBLE);
                        }
                    }else{
                        findViewById(R.id.djjdLay).setVisibility(View.GONE);
                    }

                    name.setText(linkedTreeMap.get("NAME")+"");
                    if (linkedTreeMap.containsKey("TAB")){
                        String[] split = (linkedTreeMap.get("TAB")+"").split(",");
                        if (split.length == 1){
                            ls.setVisibility(View.VISIBLE);
                            ls2.setVisibility(View.GONE);
                            ls3.setVisibility(View.GONE);
                            ls.setText(split[0]);
                        }
                        if (split.length == 2){
                            ls.setVisibility(View.VISIBLE);
                            ls2.setVisibility(View.VISIBLE);
                            ls3.setVisibility(View.GONE);
                            ls.setText(split[0]);
                            ls2.setText(split[1]);
                        }
                        if (split.length == 3){
                            ls.setVisibility(View.VISIBLE);
                            ls2.setVisibility(View.VISIBLE);
                            ls3.setVisibility(View.VISIBLE);
                            ls.setText(split[0]);
                            ls2.setText(split[1]);
                            ls3.setText(split[2]);
                        }
                    }else{
                        ls.setVisibility(View.VISIBLE);
                        ls.setText("单体门店");
                        ls2.setVisibility(View.GONE);
                        ls3.setVisibility(View.GONE);
                    }

                    if (!(linkedTreeMap.get("IMG_URL_ONE")+"").equals("null")){
                        Glide.with(context).load(HttpAdapter.HEAD_URL+linkedTreeMap.get("IMG_URL_ONE")).placeholder(R.mipmap.detail_wait).error(R.mipmap.detail_error).into(mdImg);
                    }

                    localImages = new ArrayList<>();
                    if (linkedTreeMap.containsKey("IMG_URL_ONE")){
                        localImages.add(HttpAdapter.HEAD_URL +linkedTreeMap.get("IMG_URL_ONE"));
                    }
                    if (linkedTreeMap.containsKey("IMG_URL_TWO")){
                        localImages.add(HttpAdapter.HEAD_URL +linkedTreeMap.get("IMG_URL_TWO"));
                    }
                    if (linkedTreeMap.containsKey("IMG_URL_THREE")){
                        localImages.add(HttpAdapter.HEAD_URL +linkedTreeMap.get("IMG_URL_THREE"));
                    }
                    if (linkedTreeMap.containsKey("IMG_URL_FOUR")){
                        localImages.add(HttpAdapter.HEAD_URL +linkedTreeMap.get("IMG_URL_FOUR"));
                    }

                    convenientBanner.setPages(new CBViewHolderCreator() {
                        @Override
                        public Object createHolder() {
                            return new LocalImageHolderView();
                        }
                    },localImages)
                            //设置指示器是否可见
                            .setPointViewVisible(true)
                            //设置自动切换（同时设置了切换时间间隔）
                            .startTurning(3000)
                            //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                            .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                            //设置指示器的方向（左、中、右）
                            .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
                            //设置点击监听事件
                            //                .setOnItemClickListener(this)
                            //设置手动影响（设置了该项无法手动切换）
                            .setManualPageable(true);
                    if (localImages.size() == 0){
                        mdImg.setVisibility(View.VISIBLE);
                        convenientBanner.setVisibility(View.GONE);
                    }else{
                        mdImg.setVisibility(View.GONE);
                        convenientBanner.setVisibility(View.VISIBLE);
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    //为了方便改写，来实现复杂布局的切换
    private class LocalImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            //你可以通过layout文件来创建，不一定是Image，任何控件都可以进行翻页
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            Glide.with(context).load(data).placeholder(R.mipmap.detail_wait).error(R.mipmap.detail_error).into(imageView);
        }
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        rocordgroupa();
        queryBranch();
        appbranchactivity();
        msgList();
        pfl_root.refreshComplete();
    }

    private void appbranchactivity() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appnbnactivity(BRANCH_ID, "2");
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    ArrayList<LinkedTreeMap> arrayList = (ArrayList<LinkedTreeMap>) linkedTreeMap.get("data");
                    if (arrayList.size() == 0){
                        zdList.setVisibility(View.GONE);
                    }
                    compaignAdapter = new CompaignAdapter(context, arrayList);
                    zdList.setAdapter(compaignAdapter);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    //网吧会员信息
    private void rocordgroupa() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().memberinfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    LinkedTreeMap linkedTreeMapMember = (LinkedTreeMap) linkedTreeMap.get("member");
                    nameId.setText(linkedTreeMapMember.get("name")+"");
                    yeId.setText(linkedTreeMapMember.get("level")+"，余额：￥"+linkedTreeMapMember.get("remain"));
                    findViewById(R.id.mdhyLay).setVisibility(View.VISIBLE);

                    netFreeBranchModel.setBranch_id(BRANCH_ID);
                    netFreeBranchModel.setBranch_name(zoneName);
                    netFreeBranchModel.setId_card(linkedTreeMapMember.get("id_card")+"");
                    netFreeBranchModel.setName(linkedTreeMapMember.get("name")+"");
                    netFreeBranchModel.setLevel(linkedTreeMapMember.get("level")+"");
                    netFreeBranchModel.setRemain(linkedTreeMapMember.get("remain")+"");
                    netFreeBranchModel.setToken(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
                    isonline();
                }else{
                    findViewById(R.id.mdhyLay).setVisibility(View.GONE);
                }
            }
        });
    }

    private void balance() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().balance(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, userModelBaseResultModel.getResultMsg(), 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                    rocordgroupa();
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void isonline() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().isonline(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if (decimalFormat.format(linkedTreeMap.get("code")).equals("200")){
                        LinkedTreeMap linkedTreeMapData = (LinkedTreeMap) linkedTreeMap.get("data");
                        isSj = true;
                        netFreeBranchModel.setSj(true);
                        if ((linkedTreeMapData.get("computer")+"").equals("")){
                            statusId.setText("已激活");
                            jqhTxt.setText("");
                            netFreeBranchModel.setComputer("");
                            netFreeBranchModel.setStatus("已激活");
                        }else{
                            statusId.setText("上机中");
                            jqhTxt.setText("机器号："+linkedTreeMapData.get("computer")+",已消费："+linkedTreeMapData.get("remain_fee"));
                            netFreeBranchModel.setComputer("机器号："+linkedTreeMapData.get("computer")+",已消费："+linkedTreeMapData.get("remain_fee"));
                            netFreeBranchModel.setStatus("上机中");
                        }
                    }else{
                        statusId.setText("未上机");
                        jqhTxt.setText("");
                        isSj = false;
                        netFreeBranchModel.setSj(false);
                        netFreeBranchModel.setComputer("");
                        netFreeBranchModel.setStatus("未上机");
                    }
                    netFreeBranchModel.setTime(nowTime);
                    netFreeBranchModelHashMap.put(BRANCH_ID, netFreeBranchModel);
                    BaseApplication.getInstance().setNetFreeBranchModelHashMap(netFreeBranchModelHashMap);
                }
            }
        });
    }


    private void addAttention() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().addAttention(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort("关注成功！");
                    queryBranch();
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void delAttention() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().delAttention(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort("取消关注成功！");
                    queryBranch();
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void msgList() {
        Call<ResultMessageObjectModel> call = HttpAdapter.getSerives().msgList("", "5", "1",BRANCH_ID, "", "", "3");
        call.enqueue(new OnResponseListener<ResultMessageObjectModel>(context) {
            @Override
            protected void onSuccess(ResultMessageObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getCode() == 200){
                    numLy.setText(userModelBaseResultModel.getTotal()+"留言");
                    yhly.setText("用户留言（"+ userModelBaseResultModel.getTotal()+"）");
                    ArrayList<LinkedTreeMap> arrayList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
                    messageAdapter = new MessageAdapter(context, arrayList);
                    demo_listview.setAdapter(messageAdapter);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1){
            if (resultCode == 1){
                rocordgroupa();
            }
        }
        if (requestCode == 5){
            if (resultCode == 1){
                msgList();
            }
        }
    }
}
